import { _decorator, Component, Input, input, instantiate, math, Node, Prefab } from 'cc';
import { GameManager } from './GameManager';
import { Enemy } from './Enemy';
const { ccclass, property } = _decorator;

@ccclass('EnemyManager')
export class EnemyManager extends Component {
    private static instance:EnemyManager;
    public static getInstance() {
        return this.instance;
    }
    @property
    enemy0SpawnRate: number = 1;
    @property(Prefab)
    enemy0Prefab: Prefab = null;

    @property
    enemy1SpawnRate: number = 3;
    @property(Prefab)
    enemy1Prefab: Prefab = null;

    @property
    enemy2SpawnRate: number = 10;
    @property(Prefab)
    enemy2Prefab: Prefab = null;

    @property
    rewardSpawnRate: number = 15;
    @property(Prefab)
    reward1Prefab: Prefab = null;
    @property(Prefab)
    reward2Prefab: Prefab = null;

    @property
    doubleClickInterval: number = 0.2;
    lastClickTime: number = 0;

    @property([Node])
    enemyList: Node[] = [];

    protected onLoad(): void {
        EnemyManager.instance = this;
        input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    onTouchEnd() {
        const currentTime = Date.now();
        const interval = (currentTime - this.lastClickTime)/1000;
        if(interval < this.doubleClickInterval){
            this.onDoubleClick();
        }
        this.lastClickTime = currentTime;
    }

    onDoubleClick() {
        if(GameManager.getInstance().hasBomb()){
            GameManager.getInstance().useBomb();
            for(let enemy of this.enemyList){
                enemy.getComponent(Enemy).die();
            }
        }
    }
    
    start() {
        this.schedule(this.enemy0Spawn, this.enemy0SpawnRate);
        this.schedule(this.enemy1Spawn, this.enemy1SpawnRate);
        this.schedule(this.enemy2Spawn, this.enemy2SpawnRate);
        this.schedule(this.rewardSpawn, this.rewardSpawnRate);
    }
    protected onDestroy(): void {
        this.unschedule(this.enemy0Spawn);
        this.unschedule(this.enemy1Spawn);
        this.unschedule(this.enemy2Spawn);
        this.unschedule(this.rewardSpawn);
        input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }
    enemy0Spawn() {
        this.enemySpawn(this.enemy0Prefab, -215, 215, 450)
    }
    enemy1Spawn() {
        this.enemySpawn(this.enemy1Prefab, -200, 200, 480)
    }
    enemy2Spawn() {
        this.enemySpawn(this.enemy2Prefab, -155, 155, 560)
    }

    rewardSpawn() {
        const randomInt =math.randomRangeInt(0, 2);
        let prefab = null;
        if(randomInt === 0){
            prefab = this.reward1Prefab;
        }else{
            prefab = this.reward2Prefab;
        }
        this.enemySpawn(prefab, -210, 210, 470);
    }

    enemySpawn(enemyPrefab: Prefab, minX: number, maxX: number, y: number) {
        const enemy = instantiate(enemyPrefab);
        this.node.addChild(enemy);
        if(enemy.getComponent(Enemy)){
            this.enemyList.push(enemy);
        }
        
        const randomX = math.randomRangeInt(minX, maxX);
        enemy.setPosition(randomX, y, 0);
    }

    removeEnemy(n: Node) {
        const index = this.enemyList.indexOf(n);
        if(index !== -1){
            this.enemyList.splice(index, 1);
        }
    }

    update(deltaTime: number) {
        
    }
}


